#include "popuplistwidget.h"

PopupListWidget::PopupListWidget(QWidget*parent):QListWidget(parent)
{
    setWindowFlags(Qt::Tool|Qt::FramelessWindowHint|Qt::Window);
    setStyleSheet("QListWidget{"
                  "border:none;"
                  "border-radius:10px;"
                  "background:#f0f3f6;"
                  "padding:10px;"
                  "}");
    hide();
}
bool PopupListWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 如果点击的位置不在窗口内，关闭窗口
        if (!this->geometry().contains(mouseEvent->globalPosition().toPoint()))
        {
            close();
        }
    }
    return QListWidget::eventFilter(obj, event);
}

void PopupListWidget::showEvent(QShowEvent *event)
{
    QListWidget::showEvent(event);
    // 安装事件过滤器
    qApp->installEventFilter(this);
}

void PopupListWidget::hideEvent(QHideEvent *event)
{
    QListWidget::hideEvent(event);
    // 移除事件过滤器
    qApp->removeEventFilter(this);
}

PopupWidget::PopupWidget(QWidget*parent):QWidget(parent)
{
    hide();
}
bool PopupWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 如果点击的位置不在窗口内，关闭窗口
        if (!this->geometry().contains(mouseEvent->globalPosition().toPoint()))
        {
            close();
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PopupWidget::showEvent(QShowEvent *event)
{
    QWidget::showEvent(event);
    // 安装事件过滤器
    qApp->installEventFilter(this);
}

void PopupWidget::hideEvent(QHideEvent *event)
{
    QWidget::hideEvent(event);
    // 移除事件过滤器
    qApp->removeEventFilter(this);
}

PopupTextEdit::PopupTextEdit(QWidget*parent):QTextEdit(parent)
{
    setWindowFlags(Qt::Tool|Qt::FramelessWindowHint|Qt::Window);
    hide();
}

bool PopupTextEdit::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 如果点击的位置不在窗口内，关闭窗口
        if (!this->geometry().contains(mouseEvent->globalPosition().toPoint()))
        {
            close();
        }
    }
    return QWidget::eventFilter(obj, event);
}

void PopupTextEdit::showEvent(QShowEvent *event)
{
    QTextEdit::showEvent(event);
    qApp->installEventFilter(this);
}

void PopupTextEdit::hideEvent(QHideEvent *event)
{
    QTextEdit::hideEvent(event);
    // 移除事件过滤器
    qApp->removeEventFilter(this);
}
